import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../common/models/ai/index.dart';
import '../../../../common/routers/routes.dart';
import 'index.dart';

class AiSearchPage extends GetView<AiSearchController> {
  const AiSearchPage({Key? key}) : super(key: key);

  static Future<dynamic> openSearch(List<AIClueInfo> list) async {
    return await Get.toNamed(O2OARoutes.appAiSearch,
        arguments: {'clueList': list});
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AiSearchController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: searchTitle(context)),
          body: SafeArea(
            child: Padding(
                padding: const EdgeInsets.all(15),
                child: Obx(() => controller.state.searchClueList.isNotEmpty
                    ? Container(
                        width: double.infinity,
                        decoration: BoxDecoration(
                            borderRadius:
                                const BorderRadius.all(Radius.circular(10)),
                            color: Theme.of(context).colorScheme.surface),
                        child: ListView(
                          children: controller.state.searchClueList.map((clue) {
                            return ListTile(
                              onTap: () => controller.clickPickerClue(clue),
                              title: Text(clue.title ?? ''),
                            );
                          }).toList(),
                        ))
                    : Center(
                        child: Text('empty_data'.tr,
                            style: Theme.of(context).textTheme.bodySmall)))),
          ),
        );
      },
    );
  }

  Widget searchTitle(BuildContext context) {
    return Container(
      height: 36,
      decoration: BoxDecoration(
          color: Theme.of(context).scaffoldBackgroundColor,
          borderRadius: const BorderRadius.all(Radius.circular(18))),
      alignment: Alignment.centerLeft,
      child: Padding(
        padding: const EdgeInsets.only(left: 10),
        child: Row(
          children: [
            SizedBox(
              width: 22,
              height: 22,
              child: Icon(Icons.search,
                  color: Theme.of(context).colorScheme.primary),
            ),
            Expanded(
              flex: 1,
              child: Padding(
                padding: const EdgeInsets.only(left: 5),
                child: TextField(
                  autofocus: true,
                  focusNode: controller.searchNode,
                  controller: controller.searchController,
                  decoration: InputDecoration(
                    isDense: true,
                    border: InputBorder.none,
                    hintText: 'search'.tr,
                    hintStyle: Theme.of(context).textTheme.bodySmall,
                  ),
                  style: Theme.of(context).textTheme.bodyMedium,
                  textInputAction: TextInputAction.search,
                  onSubmitted: controller.onSearchClue,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
